package top.huxy.boot.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.huxy.boot.config.Constant;
import top.huxy.boot.controller.admin.AdminBackgroundImageController;
import top.huxy.boot.exception.RequestAbortedException;
import top.huxy.boot.mapper.BackgroundImageMapper;
import top.huxy.boot.model.entity.BackgroundImage;
import top.huxy.boot.service.BackgroundImageService;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @author HuXY
 * @since 2022-01-06 10:01:16
 */
@Service
public class BackgroundImageServiceImpl implements BackgroundImageService {
    @Autowired
    private BackgroundImageMapper backgroundImageMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<BackgroundImage> getBgList() {
        return backgroundImageMapper.getBgList();
    }

    @Override
    public void addBg(AdminBackgroundImageController.Bg bg) {
        BackgroundImage variousBg = new BackgroundImage();
        variousBg.setSrc(bg.getSrc());
        int res = 0;
        try {
            res = backgroundImageMapper.addBg(variousBg);
        } catch (Exception e) {
            throw new RequestAbortedException("插入失败");
        }
        if (res == 0) {
            throw new RequestAbortedException("未插入数据");
        }
        if (res != 1) {
            throw new RequestAbortedException("不是期待的结果:" + res);
        }
    }

    @Override
    public void updateBg(AdminBackgroundImageController.Bg bg) {
        int res = 0;
        try {
            res = backgroundImageMapper.updateBg(bg);
        } catch (Exception e) {
            throw new RequestAbortedException("修改失败");
        }
        if (res == 0) {
            throw new RequestAbortedException("未修改数据");
        }
        if (res != 1) {
            throw new RequestAbortedException("不是期待的结果:" + res);
        }
    }

    @Override
    public void delBg(String id) {
        int res = 0;
        try {
            res = backgroundImageMapper.delBg(id);
        } catch (Exception e) {
            throw new RequestAbortedException("删除失败");
        }
        if (res == 0) {
            throw new RequestAbortedException("未删除数据");
        }
        if (res != 1) {
            throw new RequestAbortedException("不是期待的结果:" + res);
        }
    }

    @Override
    public String getCurrentBg() {
        Object o = redisTemplate.opsForValue().get(Constant.BACKGROUND_IMAGE_URL);
        if (o != null) {
            return o.toString();
        }
        Integer i = backgroundImageMapper.getBgCount();
        int index = ThreadLocalRandom.current().nextInt(i);
        String src = backgroundImageMapper.getBgSrcByIndex(index);
        redisTemplate.opsForValue().set(Constant.BACKGROUND_IMAGE_URL, src, 1L, TimeUnit.DAYS);
        return src;
    }

    @Override
    public void setCurrent(String id) {
        String src = backgroundImageMapper.getBgSrcById(id);
        if (src == null) {
            throw new RequestAbortedException("没有该图片");
        }
        redisTemplate.opsForValue().set(Constant.BACKGROUND_IMAGE_URL, src, 1L, TimeUnit.DAYS);
    }
}

